import allure
from pywinauto.keyboard import send_keys

from constants.constant_config import APP_PATH, APP_DIR, MAIN_WINDOW
from utils.program import Program

"""
总线相关的操作
"""


@allure.title("添加总线")
def add_bus(bus_name, bus_type, bus_index=1):
    program = Program(APP_PATH, APP_DIR, MAIN_WINDOW)
    app = program.connect_program()

    app.window(title=MAIN_WINDOW).child_window(title="项目操作", control_type="MenuItem").click_input()
    app.window(control_type="Menu").child_window(title="总线信息", control_type="MenuItem").click_input()
    app.window(title=MAIN_WINDOW).child_window(title="新增", control_type="Button").click_input()
    # 总线的第几条数,索引从0开始
    index = (bus_index - 1) * 7

    if bus_type == "1394CC":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).double_click_input()
        send_keys(bus_name)
    elif bus_type == "1394CCDL":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).double_click_input()
        send_keys(bus_name)
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index + 1).click_input()
        send_keys('{DOWN}')
        send_keys('{ENTER}')
    elif bus_type == "1553":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).click_input()
        send_keys(bus_name)
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index + 1).click_input()
        send_keys('{DOWN 4}')
        send_keys('{ENTER}')
    elif bus_type == "422":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).click_input()
        send_keys(bus_name)
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index + 1).click_input()
        send_keys('{DOWN 5}')
        send_keys('{ENTER}')
    elif bus_type == "TTE":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).click_input()
        send_keys(bus_name)
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index + 1).click_input()
        send_keys('{DOWN 7}')
        send_keys('{ENTER}')
    elif bus_type == "485":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).click_input()
        send_keys(bus_name)
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index + 1).click_input()
        send_keys('{DOWN 6}')
        send_keys('{ENTER}')
    elif bus_type == "429":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).click_input()
        send_keys(bus_name)
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index + 1).click_input()
        send_keys('{DOWN 8}')
        send_keys('{ENTER}')
    elif bus_type == "659":
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index).click_input()
        send_keys(bus_name)
        app.window(title=MAIN_WINDOW).child_window(control_type="DataItem", found_index=index + 1).click_input()
        send_keys('{DOWN 9}')
        send_keys('{ENTER}')

    app.window(title=MAIN_WINDOW).child_window(title="保存", control_type="Button").click_input()


# 新增所有总线
def add_all_bus():
    add_bus("1394CC总线", "1394CC", 1)
    add_bus("1394CCDL总线", "1394CCDL", 2)
    add_bus("1553总线", "1553", 3)
    add_bus("422总线", "422", 4)
    add_bus("TTE总线", "TTE", 5)
    add_bus("485总线", "485", 6)
    add_bus("429总线", "429", 7)
    add_bus("659总线", "659", 7)  # 超过7都用7的索引


if __name__ == '__main__':
    add_all_bus()
